package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.SysMenu;
import com.example.demo.entity.SysRole;
import com.example.demo.entity.SysUser;
import com.example.demo.mapper.SysUserMapper;
import com.example.demo.service.SysMenuService;
import com.example.demo.service.SysRoleService;
import com.example.demo.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.RedisUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 我的公众号：MarkerHub
 * @since 2024-05-18
 */
@Service(value = "SysUserServiceImpl")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysUserMapper sysUserMapper;
    @Lazy
    @Resource
    SysMenuService sysMenuService;
    @Resource
    RedisUtil redisUtil;

    @Override
    public SysUser getByUsername(String username) {

        SysUser sysUser = getOne(new QueryWrapper<SysUser>().eq("username", username));

        return sysUser;
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
        SysUser sysUser = sysUserMapper.selectById(userId);

        String authority = "";
        if (redisUtil.hasKey("GrantedAuthority: " + sysUser.getUsername())) {
            authority = (String) redisUtil.get("GrantedAuthority: " + sysUser.getUsername());

        } else {
            //获取角色
            List<SysRole> roles = sysRoleService.list(new QueryWrapper<SysRole>()
                    .inSql("id", "select role_id from sys_user_role where user_id = " + userId));
            if (roles.size() > 0) {
                String roleCodes = roles.stream().map(r -> "ROLE_" + r.getCode()).collect(Collectors.joining(","));
                authority = roleCodes.concat(",");
            }
            //菜单的编码权限
            List<Long> menuId = sysUserMapper.getNavMenuIds(userId);
            if (menuId.size() > 0) {
                List<SysMenu> menus = sysMenuService.listByIds(menuId);
                String menuPerms = menus.stream().map(m -> m.getPerms()).collect(Collectors.joining(","));
                authority = authority.concat(menuPerms);
            }
            redisUtil.set("GrantedAuthority: " + sysUser.getUsername(), authority, 60 * 60);

        }
        return authority;
    }

    @Override
    public void clearUserAuthorityInfo(String username) {
        redisUtil.del("GrantedAuthority: " + username);
    }

    @Override
    public void clearUserAuthorityInfoByRoleId(Long roleId) {
        List<SysUser> users = this.list(new QueryWrapper<SysUser>().
                inSql("id", "select user_id from sys_user_role where role_id = " + roleId));
       users.forEach(u ->{
           this.clearUserAuthorityInfo(u.getUsername());
       });

    }

    @Override
    public void clearUserAuthorityInfoByMenuId(Long menuId) {
         List<SysUser>  sysUsers =    sysUserMapper.listByMenuId(menuId);
         sysUsers.forEach(u -> {
             this.clearUserAuthorityInfo(u.getUsername());
         });
    }
}